/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2022-01-27 06:26:23
 * @LastEditors: GGB
 * @LastEditTime: 2022-01-30 06:57:18
 */
import {
    useEffect,
    useRef
} from 'react'

// electron模块

const { Menu, MenuItem, getCurrentWindow } = window.require('@electron/remote')

const useContextMenu = (itemArr, targetSelector,deps) => {
    let clickedElement = useRef(null)
    useEffect(() => {
        const menu = new Menu();
        // TODO 如果列表是空，则只展示新建 不展示删除和重命名
        itemArr.forEach(item => {
            menu.append(new MenuItem(item))
        })
        const handleContextMenu = (e) => {
            if (document.querySelector(targetSelector).contains(e.target)) {
                clickedElement.current = e.target
                menu.popup({ window: getCurrentWindow() })
            }
        }
        window.addEventListener('contextmenu', handleContextMenu)
        return () => {
            window.removeEventListener('contextmenu', handleContextMenu)
        }
    },deps)
    return clickedElement
}

export default useContextMenu
